import * as api from '../../services/example';
import { Toast } from 'antd-mobile';

export default {
  namespace: 'contacts',
  state: {
    data: [],
    deptData: [],
    perData: [],
    index: -1,
    allPer: [],
    loading: false,
    prevCode: [],
    selectP: []
  },
  reducers: {
    querySuccess(state, { payload }) {
      let { deptData, index, allPer, prevCode } = state;
      const { depart, person ,code} = payload;
      const ar1 = [...deptData];
      let perData = []
      index++;
      ar1.push(depart);
      sessionStorage.setItem('depart',code)
      person.forEach((element) => {
        element.depart = code
        let flag = false;
        for (let i=0;i<allPer.length;i++) {
          const item = allPer[i]
          if (element.zhName === item.zhName) {
            flag = true;
            break
          }
        }
        if (!flag) perData.push(element);
      });
      return {
        ...state,
        deptData: ar1,
        perData,
        index,
        allPer,
        prevCode: [...new Set([...prevCode,code])]
      };
    },
    loading(state, { payload }) {
      const { flag } = payload;
      return {
        ...state,
        loading: flag,
      };
    },
    back(state, { payload }) {
      let { index,data,personList } = payload;
      let { allPer, selectP } = state;
      const {perData} = data
      const arr = allPer.filter((item) => {
        return selectP.indexOf(item.enName) < 0;
      });
      index -= 1;
      return {
        ...state,
        index,
        perData,
        allPer: [...arr],
      };
    },
    init(state, { payload }) {
      const { index, clear, allPer } = state;
      return {
        ...state,
        index: -1,
        allPer: clear ? [] : allPer,
      };
    },
    confirm(state, { payload }) {
      const { allPer } = state;
      const { personList } = payload;
      const arr = allPer.filter((item) => {
        return personList.indexOf(item.enName) > -1;
      });
      return {
        ...state,
        allPer: arr,
        selectP: []
      };
    },
    hide(state, { payload }) {
      const { allPer } = state;
      return {
        ...state,
        modalVisible: false,
        allPer:[]
      };
    },
    filterPerson (state, {payload}){
     let {allPer, perData} = state
     const {personList, item} = payload
     const name = item.enName;
     if (name) {
      const index = personList.indexOf(name);
      if (index > -1) {
       allPer.splice(allPer.indexOf(item),1)
       if (item.enName) {
        const depart = sessionStorage.getItem('depart')
        if (item.depart+'' === depart+'') {
          perData.push(item)
        }
       }
     } else {
       perData.splice(perData.indexOf(item),1)
       if (item.enName) {
         allPer.push(item)
       }
      }
     }
     return {
       ...state,
       allPer,
       perData
     }
    },
    selectItem  (state, {payload}){
      const {item} = payload
      const {enName} = item
      let {selectP} = state
      const index = selectP.indexOf(enName)
      if (selectP.indexOf(enName) > -1) selectP.splice(index,1)
      else selectP.push(enName)
      return {
        ...state,
        selectP: [...new Set([...selectP])]
      }
    }
  },
  effects: {
    *query({ payload = {} }, { call, put, select }) {
      yield put({ type: 'loading', payload: { flag: true } });
      const { code, init } = payload;
      if (init) yield put({ type: 'init' });
      const result = yield call(api.queryContacts, { code });
      const { data } = result;
      yield put({
        type: 'querySuccess',
        payload: { depart: data.deptData, person: data.perData, code },
      });
      yield put({ type: 'loading', payload: { flag: false } });
    },
    *backView({ payload = {} }, { put,call, select }) {
      const { personList } = yield select(_ => _.detail);
      const { index } = payload;
      const {prevCode} = yield select(_=>_.contacts)
      const result = yield call(api.queryContacts, { code:prevCode[index-1] });
      const { data } = result;
      yield put({
        type: 'back',
        payload: { personList, index ,data},
      });
    },
    *onConfirm({ payload = {} }, { call, put, select }) {
      const { personList } = yield select(_ => _.detail);
      yield put({
        type: 'confirm',
        payload: { personList },
      });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        const { pathname } = location;
        if (pathname === '/detail') return;
        dispatch({
          type: 'query',
          payload: { init: true, code: 1 },
        });
      });
    },
  },
};
